<?php


namespace app\process;


use SplFileInfo;
use support\Log;
use support\utils\EnvUtil;
use support\utils\FileUtil;

class Task
{
    /**
     * @var array
     */
    protected array $_taskDir = [];


    /**
     * constructor.
     * @param string $task_dir
     */
    public function __construct($task_dir = [])
    {
        $this->_taskDir = $task_dir;
    }

    public function onWorkerStart()
    {
        FileUtil::fileIterator($this->_taskDir, function ($fileUrl, SplFileInfo $fileInfo) {
            $ext = $fileInfo->getExtension();
            if ($ext === 'php') {
                $class = str_replace('/', "\\", substr(substr($fileUrl, strlen(base_path())), 0, -4));
                if (method_exists($class, 'start')) {
                    (new $class)->start();
                    EnvUtil::isDev() && Log::debug("加载定时任务成功:" . $class);
                }
            }
        });
    }
}